package com.bupt.controller;

import com.bupt.bean.Event;
import com.bupt.config.QueryPageRequest;
import com.bupt.config.QueryResponseResult;
import com.bupt.config.Result;
import com.bupt.dao.EventRepository;
import com.bupt.service.EventService;
import com.bupt.service.PageService;
import com.bupt.utils.ExcelUtils;
import com.bupt.vo.EventExportVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(value="事件信息接口",description="事件信息接口，提供事件信息的分页、暂定五条描述数据、统计近七日报警日期和条数信息、将所有报警改为已处理状态、查询当前所有未处理报警总数量、将指定ID报警改为已处理状态、标记误报、报警信息导出")
@RestController
public class EventController {


    @Autowired
    EventRepository eventRepository;

    @Autowired
    private PageService pageService;

    @Autowired
    private EventService eventService;

    @ApiOperation("降序只查询五条描述数据")
    @RequestMapping(value = "/event",method = RequestMethod.GET)
    public List<Event> eventDesc() {
        ArrayList<Event> list = new ArrayList<Event>();
        List<Event> all = eventRepository.findAllByOrderByEventIdDesc();
        for (int i = 0; i <5 ; i++) {
            list.add(all.get(i));
        }
        return list;
    }

    @ApiOperation("降序分页查询页面列表")
    @ApiImplicitParams({@ApiImplicitParam(name="page",value="页码",required=true,paramType="path",dataType="int"),@ApiImplicitParam(name="size",value="每页记录数",required=true,paramType="path",dataType="int")})
    @GetMapping("/eventPage/{page}/{size}")
    public QueryResponseResult eventPage(@PathVariable("page") int page, @PathVariable("size")int size, QueryPageRequest queryPageRequest) {
        return pageService.findList(page,size,queryPageRequest);
    }

    @ApiOperation("统计最近七天报警日期和条数信息")
    @RequestMapping(value = "/eventAndCount",method = RequestMethod.GET)
    public List eventAndCount(){
        return eventService.getEventAndCount();
    }


    @ApiOperation("查询当前所有未处理报警总数量")
    @RequestMapping(value = "/eventCount",method = RequestMethod.GET)
    public int eventCount(){
        //未处理状态是1
        int status=1;
        return eventRepository.countByStatus(status);
    }

    @ApiOperation("将所有报警改为已处理状态")
    @RequestMapping(value = "/editEventStatus",method = RequestMethod.GET)
    public Result editEventStatus(){
        return eventService.editEventStatus();
    }

    @ApiOperation("将指定ID报警改为已处理状态")
    @RequestMapping(value = "/editEventStatuById{eventId}",method = RequestMethod.GET)
    public Result editEventStatuById(@PathVariable("eventId") Integer eventId){
        return eventService.editEventStatuById(eventId);
    }


    @ApiOperation("标记误报")
    @RequestMapping(value = "/editEventErrorFlagById{eventId}",method = RequestMethod.GET)
    public Result editEventFlagById(@PathVariable("eventId") Integer eventId){
        return eventService.editEventFlagById(eventId);
    }

    @ApiOperation(value = "报警信息导出",produces = "application/octet-stream")
    @RequestMapping(value="/event/exportExcelEvent", method = RequestMethod.GET,headers = "Accept=application/octet-stream")
    public void exportExcelEvent( QueryPageRequest queryPageRequest, HttpServletResponse res) throws IOException {
        List eventList = pageService.findList(0, 9999, queryPageRequest).getQueryResult().getList();

        List<EventExportVo> eventExportVoList = new ArrayList<EventExportVo>();
        for (int i = 0; i < eventList.size(); i++) {
            Map<String, Object> eventVo = (Map<String, Object>) eventList.get(i);
            EventExportVo eventExportVo = new EventExportVo();

            eventExportVo.setCreatTime(eventVo.get("creat_time").toString());
            eventExportVo.setDeviceName(eventVo.get("device_name").toString());
            eventExportVo.setEventDesc(eventVo.get("event_desc").toString());
            eventExportVo.setEventStatus(eventVo.get("eventStatus").toString());
            eventExportVo.setEventType(eventVo.get("eventType").toString());
            eventExportVo.setPosition(eventVo.get("position").toString());
            eventExportVo.setValue(eventVo.get("value").toString());
            eventExportVoList.add(eventExportVo);
        }

        ExcelUtils.exportExcel(eventExportVoList, "报警信息表", "报警信息", EventExportVo.class, "event", res);
    }

}